//
//  DownloadDelegate.m
//  ZYYD
//
//  Created by yuzhou on 12-8-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DownloadDelegate.h"
#import "Constant.h"
#import "DownloadManager.h"
#import "DownloadInfo.h"

@implementation DownloadDelegate
@synthesize infoDic,downloadInfo;

- (void)setProgress:(CGFloat)progress
{
//    NSLog(@"id:%@,progress:%f",[infoDic valueForKey:@"item_id"],progress);
    [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateProgressInShelfCell object:nil];
    self.downloadInfo.lastPercent = progress;
}

- (void)requestStarted:(ASIHTTPRequest *)request
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateDownloadStatus object:nil];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@">>>下载完成<<<");
    
    self.downloadInfo.downloaded = YES;
    [[DownloadManager defaultManager] downloadNext];
    [[NSNotificationCenter defaultCenter] postNotificationName:kOneDownloadFinished object:self.downloadInfo];
    [[DownloadManager defaultManager] save];
    [self release];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@">>>下载失败<<<%d",request.error.code);
//    [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateShelfGridView object:nil];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"下载失败"] autorelease];
    [promptView show];
    [[DownloadManager defaultManager] setDownloadingDic:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateDownloadStatus object:nil];
    
    [self release];
}

- (DownloadInfo *)downloadInfo
{
    if(downloadInfo != nil){
        return downloadInfo;
    }
    int index = [[DownloadManager defaultManager] indexOfID:[infoDic valueForKey:@"subitem_id"]];
    downloadInfo = [[[[DownloadManager defaultManager] allDownload] objectAtIndex:index] retain];
    return downloadInfo;
}

- (void)dealloc
{
    [infoDic release];
    [downloadInfo release];
    [super dealloc];
}
@end
